﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xf.Entitys;
using xf.Service;

namespace xf
{
    public partial class FSaveGoods : Form
    {
        public FSaveGoods()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 关闭新增、修改产品信息窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelAddCustomer_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        /// <summary>
        /// 添加商品信息操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void triggerSaveCustomer_Click(object sender, EventArgs e)
        {
            if (!Double.TryParse(costUnit.Text.Trim(), out double costUnitValue) || 
                (costUnitValue <= 0) ||
                (costUnitValue > 999999.99) ||
                !Decimal.TryParse(costUnit.Text.Trim(), out decimal costUnitDecimal)
                )
            { 
                MessageBox.Show("非法产品价格，必须为浮点型，保留两位有效数字");
                return;
            }

            Dictionary<string, string> queryParam = new Dictionary<string, string> {
                { "Goods_id", goodsId.Text.Trim()},
                { "Goods_name", goodsName.Text.Trim()},
            };
            GOODS_NAME goodsNameObj = new GOODS_NAME() {
                Goods_id = goodsId.Text.Trim(),
                Goods_name = goodsName.Text.Trim(),
                cost_unit = costUnitDecimal
            };
            Dictionary<string, string> detail = new Dictionary<string, string>();
            if (GoodsService.GetInstance().Add(goodsNameObj, ref detail))
            {
                MessageBox.Show("添加产品信息成功");
                FGoodsList.StaticReloadCustomerList();
                cancelAddCustomer_Click(sender, EventArgs.Empty);
            }
            else 
            {
                detail.TryGetValue("msg", out string msg);
                MessageBox.Show("添加产品信息失败：" + msg);
            }
        }
    }
}
